
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.BLL.EntityCollections;
using System.Threading;
using System.Xml;
using System.Net;
using System.Globalization;
using Tathata.Web.Administration.Modules;

namespace Tathata.Web.Administration
{
    public partial class Administration_Currencies : BaseNopAdministrationPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCurrencyGrid();
            }
        }

        protected void BindCurrencyGrid()
        {
            CurrencyCollection currencyCollection = CurrencyManager.GetAllCurrencies();
            gvCurrencies.DataSource = currencyCollection;
            gvCurrencies.DataBind();
        }

        protected void BindLiveRateGrid()
        {
            DataTable liveRates = null;
            DateTime liveRatesUpdateDate = DateTime.MinValue;
            CurrencyManager.GetCurrencyLiveRates(CurrencyManager.PrimaryExchangeRateCurrency.CurrencyCode, out liveRatesUpdateDate, out liveRates);
            gvLiveRates.DataSource = liveRates;
            gvLiveRates.DataBind();
        }

        protected void rdbIsPrimaryExchangeRateCurrency_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvCurrencies.Rows)
            {
                RadioButton rdbIsPrimaryExchangeRateCurrency = (RadioButton)row.FindControl("rdbIsPrimaryExchangeRateCurrency");
                HiddenField hfCurrencyID = (HiddenField)row.FindControl("hfCurrencyID");
                int currencyID = int.Parse(hfCurrencyID.Value);
                if (rdbIsPrimaryExchangeRateCurrency == sender)
                    CurrencyManager.PrimaryExchangeRateCurrency = CurrencyManager.GetByCurrencyID(currencyID);
            }
            BindCurrencyGrid();
        }

        protected void rdbIsPrimaryStoreCurrency_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvCurrencies.Rows)
            {
                RadioButton rdbIsPrimaryStoreCurrency = (RadioButton)row.FindControl("rdbIsPrimaryStoreCurrency");
                HiddenField hfCurrencyID = (HiddenField)row.FindControl("hfCurrencyID");
                int currencyID = int.Parse(hfCurrencyID.Value);
                if (rdbIsPrimaryStoreCurrency == sender)
                    CurrencyManager.PrimaryStoreCurrency = CurrencyManager.GetByCurrencyID(currencyID);
            }
            BindCurrencyGrid();
        }

        protected void btnGetLiveRates_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    BindLiveRateGrid();
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }


        protected void gvLiveRates_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ApplyLiveRate")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvLiveRates.Rows[index];
                
                Label lblCurrencyCode = row.FindControl("lblCurrencyCode") as Label;
                DecimalTextBox txtRate = row.FindControl("txtRate") as DecimalTextBox;

                Currency currency = null;
                CurrencyCollection currencies = CurrencyManager.GetAllCurrencies();
                foreach (Currency currency2 in currencies)
                    if (currency2.CurrencyCode.ToLower() == lblCurrencyCode.Text.ToLower())
                        currency = currency2;
                if (currency != null)
                {
                    CurrencyManager.UpdateCurrency(currency.CurrencyID, currency.Name, currency.CurrencyCode,
                        txtRate.Value, currency.DisplayLocale, currency.Published, currency.DisplayOrder,
                        currency.CreatedOn, DateTime.Now);
                    BindCurrencyGrid();
                }
            }
        }

        protected void gvLiveRates_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button btnApplyRate = e.Row.FindControl("btnApplyRate") as Button;
                if (btnApplyRate != null)
                    btnApplyRate.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

    }
}
